package com.tgy.atomic;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;
import java.util.concurrent.locks.LockSupport;

/**
 * @Author: tgy
 * @Date: 12/8/20 3:54 PM
 */
public class LongAddrDemo {

    public static void main(String[] args) throws InterruptedException {

        LongAdder longAdder = new LongAdder();

        AtomicInteger atomicInteger = new AtomicInteger();

        int len = 10;

        long start;

       final CountDownLatch countDownLatch = new CountDownLatch(len);
        start = System.currentTimeMillis();
        for (int i = 0; i < len; i++) {

            new Thread(()->{

                for (int j = 0; j < 10000000; j++) {

//                    atomicInteger.incrementAndGet();
                    longAdder.increment();
                }

               countDownLatch.countDown();
            },"t"+i).start();
        }

        countDownLatch.await();
//        System.out.println(atomicInteger.get());
        System.out.println(System.currentTimeMillis() - start);

        CountDownLatch countDownLatch01 = new CountDownLatch(len);

        start = System.currentTimeMillis();
        for (int i = 0; i < len; i++) {

            new Thread(()->{

                for (int j = 0; j < 10000000; j++) {

//                    longAdder.increment();
                    atomicInteger.incrementAndGet();
                }

                countDownLatch01.countDown();
            },"t"+i).start();
        }

        countDownLatch01.await();
//        System.out.println(longAdder.sum());
        System.out.println(System.currentTimeMillis() - start);
    }


}
